package tools;

import dialogmaker.ButtonManager;
import dialogmaker.Configurations;
import dialogmaker.ErrorManager;
import dialogmaker.LDialogManager;
import game.Data;
import game.Data.BlocoInfo;
import game.Data.ElementoInfo;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import saves.GasAtmosferico;

/**
 *
 * @author Luan
 */
public class InfoEditor extends JFrame {
    private static final long serialVersionUID = 1L;
    
    public static void main(String[] args) {
        setupDialogMaker();
        Data.load();
        new InfoEditor();
    }
    
    public static void setupDialogMaker() {
        ButtonManager.ADD_IMAGE = getImage("add.png");
        ButtonManager.BROWSE_IMAGE = getImage("find.png");
        ButtonManager.CLEAN_IMAGE = getImage("cancel.png");
        ButtonManager.DELETE_IMAGE = getImage("delete.png");
        ButtonManager.EDIT_IMAGE = getImage("edit.png");
        ButtonManager.UP_IMAGE = getImage("up.png");
        ButtonManager.DOWN_IMAGE = getImage("down.png");
        ButtonManager.INFO_ICON = getImage("info.png");
        ButtonManager.VAL_ICON = getImage("error.png");

    	ErrorManager.WARNINGS = true;
        Configurations.MIN_SIZE = new Dimension(250, 25);
    }
    
    public static ImageIcon getImage(String img) {
        return new ImageIcon(InfoEditor.class.getResource("/icons/" + img));
    }
    
    public InfoEditor() {
        super("InfoEditor");
        
        this.setLayout(new GridLayout(3, 1));
        this.add(new LDButton("Blocos", BlocoInfo.class, Data.getBlocos()));
        this.add(new LDButton("Gases", GasAtmosferico.class, Data.getGases()));
        this.add(new LDButton("Elementos", ElementoInfo.class, Data.getElementos()));
        
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Data.save();
                InfoEditor.this.setVisible(false);
                InfoEditor.this.dispose();
            }
        });
        
        this.setMinimumSize(new Dimension(250, 75));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
    }
    
    private class LDButton extends JButton {
        private static final long serialVersionUID = 1L;
        private String name;
        private Class cl;
        private ArrayList<?> list;
        
        public LDButton(String name, Class cl, ArrayList<?> list) {
            super(name);
            this.name = name;
            this.cl = cl;
            this.list = list;
            
            this.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new LDialogManager(InfoEditor.this, LDButton.this.name, LDButton.this.cl, LDButton.this.list);
                }
            });
        }
    }
}
